/*!
 * FileName: luogu-1967.cpp
 * This Problem is on luogu. The ID of the problem is 1967. 
 * Copyright(c) 2019 Shu_Yu_Mo
 * MIT Licensed
 * Luogu: https://www.luogu.org/space/show?uid=44615
 * Github: https://github.com/oldsuold/
 * Gitee: https://gitee.com/Shu_Yu_Mo/
 * These words were created by an amazing tool written by Shu_Yu_Mo.
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstring>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int _N = 1e5 + 100;
const int _M = 5 * 1e5 + 100; 
inline int read()
{
    char c = getchar(); int sign = 1; int x = 0;
    while(c > '9' || c < '0') { if(c=='-')sign = -1; c = getchar(); } 
    while(c <= '9' && c >= '0') { x *= 10; x += c - '0'; c = getchar(); }
    return x * sign;
}
int n, m;

int f[_N];
int find(int x) { return f[x] == x ? x :f[x] = find(f[x]); }
void initF()
{
	for(register int i = 1;i <= n;i++) f[i] = i;
}
void marge(int x, int y){
	x = find(x);
	y = find(y);
	if(x == y) return;
	f[x] = y;
}
bool ask(int x, int y) { return find(x) == find(y); }

struct _edges{
	int node;
	int w;
	int nxt;
}edge[_M];
int head[_N];
int tot = 0;


void add(int u, int v,int w) 
{
//	printf("added %d %d\n", u, v);
//	printf("%d--%d[lable=\"%d\"];\n", u, v, w);
	edge[++tot].nxt = head[u];
	edge[tot].node = v;
	edge[tot].w = w;
	head[u] = tot;

	swap(u, v);

	edge[++tot].nxt = head[u];
	edge[tot].node = v;
	edge[tot].w = w;
	head[u] = tot;
}
struct edges{
	int a;
	int b;
	int c;
}e[_M];
inline bool Rule_Of_Shu_Yu_Mo(const edges & A, const edges & B) { return A.c > B.c; }
int vis[_N];
int _minW[_N];
int nowRoot ;
void dfs(int k, int minW)
{
	if(vis[k] != 0) return;
	vis[k]     =  nowRoot;
	_minW[k]   =  minW;
	for(register int i = head[k];i != 0;i = edge[i].nxt)
		dfs(edge[i].node, min(minW, edge[i].w));
}
int main()
{
	n = read();m = read();
	initF();
	for(register int i = 1;i <= m;i++)
	{
		e[i].a = read();
		e[i].b = read();
		e[i].c = read();
	}
//	for(register int i = 1;i <= m ;i++)
//		printf("%d--%d[lable=\"%d\"];\n", e[i].a, e[i].b, e[i].c);
//	puts("");
	sort(e + 1, e + 1 + m, Rule_Of_Shu_Yu_Mo);
	for(register int i = 1;i <= m;i++)
	{
		edges &now = e[i];
		if(!ask(now.a, now.b))
			add(now.a, now.b, now.c),marge(now.a, now.b);
	}
	memset(vis, false,sizeof(vis));
	for(register int i = 1;i <= n;i++)
		if(!vis[i]) 
		{
			nowRoot = i;
			dfs(i, inf);
		}
	for(register int i = 1;i <= n;i++)
//	{
		printf("at %d, dis %d\n", vis[i], _minW[i]);
//	}
//	_minW[1] = 0;
	int Q = read();
//	for(register int i =  )
	while(Q--)
	{
		int tmpx = read() , tmpy =read();
		if(vis[tmpx] != vis[tmpy])
			puts("-1");
		else
			printf("%d\n", min(_minW[tmpx], _minW[tmpy]));
	}
    return 0;
}
